package com.runen.infinitemovement.widget.popupwind;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.runen.infinitemovement.R;
import com.runen.infinitemovement.model.entity.AnswerProgressEntity;
import com.runen.infinitemovement.tool.CommonUtil;
import com.runen.infinitemovement.widget.IndicatorSeekBar;

/**
 * Create by LiaoDuanHong
 * Create on
 */
public class PersonLongLoadPopupWindow extends PopupWindow {
    private Context context;
    private View layout;
    private TextView tv_answer_count, tv_progress, tv_start_end_name;
    private IndicatorSeekBar indicator_seek_bar_long;
    private TextView tv_surplus_progress;
    private ImageView iv_dismiss;
    private AnswerProgressEntity.DataBean entity;

    public static PersonLongLoadPopupWindow getInstance(Context context, AnswerProgressEntity.DataBean entity) {
        return new PersonLongLoadPopupWindow(context, entity);
    }

    private PersonLongLoadPopupWindow(Context context, AnswerProgressEntity.DataBean entity) {
        super(context);
        this.context = context;
        this.entity = entity;
        initView();
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(false);
        this.setOutsideTouchable(false);
        // 这样设置才能点击屏幕外dismiss窗口
        this.setBackgroundDrawable(new BitmapDrawable());
        this.setContentView(layout);

        initEvent();
    }

    private void initView() {
        layout = View.inflate(context, R.layout.popup_person_long_road, null);
        tv_answer_count = layout.findViewById(R.id.tv_answer_count);
        tv_progress = layout.findViewById(R.id.tv_progress);
        tv_start_end_name = layout.findViewById(R.id.tv_start_end_name);
        indicator_seek_bar_long = layout.findViewById(R.id.indicator_seek_bar_long);
        tv_surplus_progress = layout.findViewById(R.id.tv_surplus_progress);
        iv_dismiss = layout.findViewById(R.id.iv_dismiss);
        indicator_seek_bar_long.setEnabled(false);

        setView();
    }

    private void setView() {
        tv_start_end_name.setText(entity.startPoint + "-" + entity.endPoint);
        tv_answer_count.setText(entity.correctNumber + "");

        String string1 = CommonUtil.Double2position(entity.increaseProgressValue * 100) + "%";
        SpannableString spanColor1 = new SpannableString(string1);
        spanColor1.setSpan(new AbsoluteSizeSpan(14, true), string1.length() - 1, string1.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        tv_progress.setText(spanColor1);

        String surplus = "距离目的地剩余:%s";
        tv_surplus_progress.setText(String.format(surplus,
                CommonUtil.Double2position(entity.surplusProgressValue * 100) + "%"));

        indicator_seek_bar_long.setProgress((int) (entity.currentProgressValue * 100));
        indicator_seek_bar_long.setDoubleProgress(entity.currentProgressValue * 100);
    }


    private void initEvent() {
        iv_dismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }


    public void show() {
//        this.setAnimationStyle(R.style.Animation);
        //防止部分手机底部状态栏与popupWindow重叠
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        this.setAnimationStyle(R.style.AnimationBottomFade);
        //popupWindow底部显示
        this.showAtLocation(((Activity) context).getWindow().getDecorView().findViewById(android.R.id.content), Gravity.CENTER, 0, 0);
        WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();

        lp.alpha = 0.5f;
        if (lp.alpha == 1) {
            ((Activity) context).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//不移除该Flag的话,在有视频的页面上的视频会出现黑屏的bug
        } else {
            ((Activity) context).getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//此行代码主要是解决在华为手机上半透明效果无效的bug
        }
        ((Activity) context).getWindow().setAttributes(lp);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();
        lp.alpha = 1f;
        ((Activity) context).getWindow().setAttributes(lp);
    }
}

